package com.pym.bs.handler;


import com.pym.bs.enums.ResultEnum;
import com.pym.bs.utils.GsonUtils;
import com.pym.bs.utils.ResultVOUtils;
import com.pym.bs.vo.ResultVO;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Component("myAuthenticationFailureHandler")
public class MyAuthenticationFailureHandler implements AuthenticationFailureHandler {

    @Override
    public void onAuthenticationFailure(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e) throws IOException {
        System.out.println("登录失败");
        ResultVO resultVO = ResultVOUtils.error(ResultEnum.UNAUTHORIZED, e.getMessage());
        String error = GsonUtils.toJson(resultVO);
        httpServletResponse.setContentType("application/json;charset=UTF-8");
        httpServletResponse.getWriter().write(error);
        System.out.println(error);
    }
}
